Data passing from one view to another

  • STEPS

    1. Listing page

    See complete code of Listing page

    2. Passing data to next page

    
    
     class MyHomePage extends StatelessWidget { 
       MyHomePage({Key key, this.title}) : super(key: key); 
       final String title; 
       final items = Product.getProducts(); 
       
       @override  
       Widget build(BuildContext context) { 
          return Scaffold( appBar: AppBar(title: Text("Product Navigation")), 
          body: ListView.builder( 
             itemCount: items.length, 
             itemBuilder: (context, index) {
                return GestureDetector( 
                   child: ProductBox(item: items[index]), 
                   onTap: () { 
                      Navigator.push( 
                         context, MaterialPageRoute( 
                            builder: (context) => ProductPage(item: items[index]), 
                         ), 
                      ); 
                   }, 
                ); 
             }, 
          )); 
       } 
    }
    
    
    Here, we have used MaterialPageRoute to navigate to product details page.

    3. get passing data in next page

    
    class ProductPage extends StatelessWidget { 
       ProductPage({Key key, this.item}) : super(key: key); 
       final Product item; 
       
       @override  
       Widget build(BuildContext context) {
          return Scaffold(
             appBar: AppBar( 
                title: Text(this.item.name), 
             ), 
             body: Center(
                child: Container(
                   padding: EdgeInsets.all(0), 
                   child: Column(
                      mainAxisAlignment: MainAxisAlignment.start, 
                      crossAxisAlignment: CrossAxisAlignment.start, 
                      children: <Widget>[
                         Image.asset("assets/appimages/" + this.item.image), 
                         Expanded(
                            child: Container(
                               padding: EdgeInsets.all(5), 
                               child: Column(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
                                  children: <Widget>[
                                     Text(
                                        this.item.name, style: TextStyle(
                                           fontWeight: FontWeight.bold
                                        )
                                     ), 
                                     Text(this.item.description), 
                                     Text("Price: " + this.item.price.toString()), 
                                     RatingBox(),
                                  ], 
                               )
                            )
                         )
                      ]
                   ), 
                ), 
             ), 
          ); 
       } 
    }
    
    
    Complete code
    
    
    import 'package:flutter/material.dart'; 
    void main() => runApp(MyApp()); 
    
    class Product {
       final String name; 
       final String description; 
       final int price; 
       final String image; 
       Product(this.name, this.description, this.price, this.image); 
       
       static List<Product> getProducts() {
          List<Product> items = <Product>[]; 
          items.add(
             Product(
                "Pixel", 
                "Pixel is the most featureful phone ever", 
                800, 
                "pixel.png"
             )
          );
          items.add(
             Product(
                "Laptop", 
                "Laptop is most productive development tool", 
                2000, 
                "laptop.png"
             )
          ); 
          items.add(
             Product(
                "Tablet", 
                "Tablet is the most useful device ever for meeting", 
                1500, 
                "tablet.png"
             )
          ); 
          items.add(
             Product( 
                "Pendrive", 
                "iPhone is the stylist phone ever", 
                100, 
                "pendrive.png"
             )
          ); 
          items.add(
             Product(
                "Floppy Drive", 
                "iPhone is the stylist phone ever", 
                20, 
                "floppy.png"
             )
          ); 
          items.add(
             Product(
                "iPhone", 
                "iPhone is the stylist phone ever", 
                1000, 
                "iphone.png"
             )
          ); 
          return items; 
       }
    }
    class MyApp extends StatelessWidget {
       // This widget is the root of your application. 
       @override  
       Widget build(BuildContext context) {
          return MaterialApp(
             title: 'Flutter Demo', 
             theme: ThemeData( 
                primarySwatch: Colors.blue, 
             ), 
             home: MyHomePage(title: 'Product Navigation demo home page'), 
          ); 
       }
    }
    class MyHomePage extends StatelessWidget {
       MyHomePage({Key key, this.title}) : super(key: key); 
       final String title; 
       final items = Product.getProducts(); 
       
       @override  
       Widget build(BuildContext context) {
          return Scaffold(
             appBar: AppBar(title: Text("Product Navigation")), 
             body: ListView.builder( 
                itemCount: items.length, 
                itemBuilder: (context, index) { 
                   return GestureDetector( 
                      child: ProductBox(item: items[index]), 
                      onTap: () { 
                         Navigator.push( 
                            context, 
                            MaterialPageRoute( 
                               builder: (context) => ProductPage(item: items[index]), 
                            ), 
                         ); 
                      }, 
                   ); 
                }, 
             )
          ); 
       }
    } 
    class ProductPage extends StatelessWidget {
       ProductPage({Key key, this.item}) : super(key: key); 
       final Product item; 
       
       @override  
       Widget build(BuildContext context) {
          return Scaffold(
             appBar: AppBar(
                title: Text(this.item.name), 
             ), 
             body: Center(
                child: Container( 
                   padding: EdgeInsets.all(0), 
                   child: Column( 
                      mainAxisAlignment: MainAxisAlignment.start, 
                      crossAxisAlignment: CrossAxisAlignment.start, 
                      children: <Widget>[ 
                         Image.asset("assets/appimages/" + this.item.image), 
                         Expanded( 
                            child: Container( 
                               padding: EdgeInsets.all(5), 
                               child: Column( 
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
                                  children: <Widget>[ 
                                     Text(this.item.name, style: TextStyle(fontWeight: FontWeight.bold)), 
                                     Text(this.item.description), 
                                     Text("Price: " + this.item.price.toString()), 
                                     RatingBox(), 
                                  ], 
                               )
                            )
                         ) 
                      ]
                   ), 
                ), 
             ), 
          ); 
       } 
    }
    
    
    
    class RatingBox extends StatefulWidget { 
       @override  
       _RatingBoxState createState() => _RatingBoxState(); 
    } 
    
    
    
    class _RatingBoxState extends State<RatingBox> { 
       int _rating = 0;
       void _setRatingAsOne() {
          setState(() {
             _rating = 1; 
          }); 
       }
       void _setRatingAsTwo() {
          setState(() {
             _rating = 2; 
          }); 
       } 
       void _setRatingAsThree() { 
          setState(() {
             _rating = 3; 
          }); 
       }
       Widget build(BuildContext context) {
          double _size = 20; 
          print(_rating); 
          return Row(
             mainAxisAlignment: MainAxisAlignment.end, 
             crossAxisAlignment: CrossAxisAlignment.end, 
             mainAxisSize: MainAxisSize.max, 
             children: <Widget>[
                Container(
                   padding: EdgeInsets.all(0), 
                   child: IconButton(
                      icon: (
                         _rating >= 1 ? Icon( 
                            Icons.star, 
                            size: _size, 
                         ) 
                         : Icon( 
                            Icons.star_border, 
                            size: _size, 
                         )
                      ), 
                      color: Colors.red[500], 
                      onPressed: _setRatingAsOne, 
                      iconSize: _size, 
                   ), 
                ), 
                Container(
                   padding: EdgeInsets.all(0), 
                   child: IconButton( 
                      icon: (
                         _rating >= 2 ? 
                         Icon( 
                            Icons.star, 
                            size: _size, 
                         ) 
                         : Icon( 
                            Icons.star_border, 
                            size: _size, 
                         )
                      ), 
                      color: Colors.red[500], 
                      onPressed: _setRatingAsTwo, 
                      iconSize: _size, 
                   ), 
                ), 
                Container(
                   padding: EdgeInsets.all(0), 
                   child: IconButton(
                      icon: (
                         _rating >= 3 ? 
                         Icon( 
                            Icons.star, 
                            size: _size, 
                         )
                         : Icon( 
                            Icons.star_border, 
                            size: _size, 
                         )
                      ), 
                      color: Colors.red[500], 
                      onPressed: _setRatingAsThree, 
                      iconSize: _size, 
                   ), 
                ), 
             ], 
          ); 
       } 
    } 
    class ProductBox extends StatelessWidget {
       ProductBox({Key key, this.item}) : super(key: key); 
       final Product item; 
       
       Widget build(BuildContext context) {
          return Container(
             padding: EdgeInsets.all(2), 
             height: 140, 
             child: Card(
                child: Row(
                   mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
                   children: <Widget>[ 
                      Image.asset("assets/appimages/" + this.item.image), 
                      Expanded( 
                         child: Container( 
                            padding: EdgeInsets.all(5), 
                            child: Column( 
                               mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
                               children: <Widget>[ 
                                  Text(this.item.name, style: TextStyle(fontWeight: FontWeight.bold)), Text(this.item.description), 
                                  Text("Price: " + this.item.price.toString()), 
                                  RatingBox(), 
                               ], 
                            )
                         )
                      ) 
                   ]
                ), 
             )
          ); 
       } 
    }